var app = getApp();
var openidXcx = '';
var unionid = '';
Page({
  data: {
    //show: "",
  },
  sco:function(){
    var value = wx.getStorageSync('userInfo')
    console.log("value:" + JSON.stringify(value));
    //console.log("unionid:" + value.unionid);
    //console.log("openid:" + value.openId);
    if (value) {
      openidXcx = value.openId
      unionid = value.unionid
    } else {
      wx.reLaunch({
        url: '../index2/index2'
      })
    }
    /** 初始化条用扫码方法 */
    var that = this;
    var show;
    var path;
    var deivceid;
    wx.scanCode({
      success: (res) => {
        this.show = "--result:" + res.result + "--scanType:" + res.scanType + "--charSet:" + res.charSet + "--path:" + res.path;
        this.path = res.path;
        deivceid = res.path.substring(24);
        console.log("res-----" + JSON.stringify(res))
        console.log("deivceid-----" + deivceid)
        console.log("unionid-----" + unionid)
        that.setData({
          show: this.show
        })
        wx.request({
          url: 'https://wwcxc.arksh.com/api/xcx/scan', //仅为示例，并非真实的接口地址
          data: {
            unionid: unionid,
            deivceid: deivceid,
            nid: 35,
          },
          success(res) {
            console.log("that.path：" + JSON.stringify(that.path))
            console.log("小程序扫码：" + JSON.stringify(res.data))
            var ret = res.data.ret;
            if (ret == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 5000
              })
            } else if (ret == 4) {
              console.log("小程序扫码设备未开机")
              wx.reLaunch({
                url: '/pages/pay/pay?openid=' + openidXcx + '&device=' + deivceid + "&hotel=" + res.data.hotel.company + "&devices=" + res.data.device.noid + "&hotels=" + res.data.hotel.id
              })
            } else {
              wx.reLaunch({
                url: '/' + that.path,
              })
            }
          }
        })

      },

      fail: (res) => {
        wx.showToast({
          title: '未识别到二维码',
          icon: 'none',
          duration: 2000
        })
      },
      complete: (res) => {
      }
    })
    console.log("path:" + path)
    console.log("show:" + show)
  },
  onLoad: function () {
  }
})